<?php

class PaymentController extends Controller
{

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules() {
        return array(
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions' => array('index','suggesttags'),
                //'users' => UserIdentity::getAccessUser(5),
                'users' => array('veniceq'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
      
		);
	}

	/**
	 * Lists all models.
	 */
       
	public function actionIndex()
	{
		$model=new TransactionModel;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['TransactionModel']))
		{
			$model->attributes=$_POST['TransactionModel'];
                        if($model->tag){
                            $tag = TagModel::model()->string2array($model->tag);
                            TagModel::model()->addTags($tag);
                        }
                        
                        $model->amount      = floatval($model->amount);
                        $model->modified    = date("Y-m-d H:i:s");
			if($model->save()){
                                $Source = SourceModel::model()->findByPk($model->source);   
                                $Source->amount = floatval($Source->amount + $model->amount);
                                $Source->update();
                                if($model->destination){
                                    $destination = SourceModel::model()->findByPk($model->destination);   
                                    $destination->amount = floatval($destination->amount + ($model->amount*(-1)));
                                    $destination->update();
                                }
                                
				$this->redirect(array("/admin/$model->id?model=transaction"));
                        }        
		}

		$this->render('index',array(
			'model'=>$model,
		));
	}
        
        /**
	 * Suggests tags based on the current user input.
	 * This is called via AJAX when the user is entering the tags input.
	 */
        
	public function actionSuggestTags()
	{
		if(isset($_GET['q']) && ($keyword=trim($_GET['q']))!=='')
		{
			$tags=TagModel::model()->suggestTags($keyword);
			if($tags!==array())
				echo implode("\n",$tags);
		}
	}
        
        public function actionWithDrawTransaction()
	{
            $id = $_GET['id'];
            TransactionModel::model()->findByPk($id);
        }
        

}
